package com.geovis.emergency.spd.biz.pc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.pc.mapper.PcPlanDutyDeptMapper;
import com.geovis.emergency.spd.biz.pc.service.IPcPlanDutyDeptService;
import com.geovis.emergency.spd.biz.system.service.ISystemDeptService;
import com.geovis.emergency.spd.cache.DeptCache;
import com.geovis.emergency.spd.cache.SysCache;
import com.geovis.emergency.spd.entity.pc.entity.PcPlanDutyDept;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcPlanConstant;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcPlanDutyDeptVO;
import com.geovis.emergency.spd.entity.system.entity.SystemDept;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 应急预案指挥模块 -  预案成员单元责任 服务实现类
 * </p>
 *
 * @author Lonsho
 * @since 2022-07-11
*/

@Service
@RequiredArgsConstructor
@Slf4j
public class PcPlanDutyDeptServiceImpl extends ServiceImpl<PcPlanDutyDeptMapper, PcPlanDutyDept> implements IPcPlanDutyDeptService {
    private final ISystemDeptService systemDeptService;

    @Override
    public void savePlanDutyDept(List<PcPlanDutyDeptVO> dutyDeptList, String pcPlanId) {
        for (PcPlanDutyDeptVO pcPlanDutyDeptVO : dutyDeptList) {
            PcPlanDutyDept pcPlanDutyDept = baseMapper.selectOne(
                    Wrappers.lambdaQuery(PcPlanDutyDept.class)
                            .eq(PcPlanDutyDept::getDeptId, pcPlanDutyDeptVO.getDeptId())
                            .eq(PcPlanDutyDept::getPcPlanId, pcPlanId)
                            .last("limit 1")
            );
            if (ObjectUtil.isNull(pcPlanDutyDept)) {
                //if (StrUtil.isNotBlank(pcPlanDutyDeptVO.getDuty())){
                    PcPlanDutyDept newPcPlanDutyDept = BeanUtil.toBean(pcPlanDutyDeptVO, PcPlanDutyDept.class);
                    newPcPlanDutyDept.setPcPlanId(pcPlanId);
                    baseMapper.insert(newPcPlanDutyDept);
                //}
            }else {
                BeanUtil.copyProperties(pcPlanDutyDeptVO, pcPlanDutyDept);
                baseMapper.updateById(pcPlanDutyDept);
            }
        }
    }

    @Override
    public List<PcPlanDutyDeptVO> selectPlanDuty(String pcPlanId) {
        List<PcPlanDutyDeptVO> pcPlanDutyDeptVOList = Lists.newArrayList();
        String deptName = SysCache.getParamValue(PcPlanConstant.DUTY_DEPT);
        if (ObjectUtil.isNotNull(deptName)){
            SystemDept systemDept = DeptCache.getDeptByName(deptName.trim());
            if (ObjectUtil.isNotNull(systemDept)){
                List<SystemDept> deptList = systemDeptService.list(
                        Wrappers.lambdaQuery(SystemDept.class)
                                .eq(SystemDept::getParentId, systemDept.getId())
                                .orderByAsc(SystemDept::getIdx)
                );
                List<PcPlanDutyDept> pcPlanDutyDeptList = Lists.newArrayList();
                if (StrUtil.isNotBlank(pcPlanId)){
                    pcPlanDutyDeptList = baseMapper.selectList(
                            Wrappers.lambdaQuery(PcPlanDutyDept.class)
                                    .eq(PcPlanDutyDept::getPcPlanId, pcPlanId)
                    );
                }
                List<PcPlanDutyDept> finalPcPlanDutyDeptList = pcPlanDutyDeptList;
                pcPlanDutyDeptVOList = deptList.stream().map(dept -> {
                    PcPlanDutyDeptVO pcPlanDutyDeptVO = new PcPlanDutyDeptVO();
                    pcPlanDutyDeptVO.setDeptId(dept.getId());
                    pcPlanDutyDeptVO.setDeptName(dept.getDeptName());
                    Optional<PcPlanDutyDept> first = finalPcPlanDutyDeptList.stream().filter(s -> s.getDeptId().equals(dept.getId())).findFirst();
                    first.ifPresent(pcPlanDutyDept -> pcPlanDutyDeptVO.setDuty(pcPlanDutyDept.getDuty()));
                    first.ifPresent(pcPlanDutyDept -> pcPlanDutyDeptVO.setId(pcPlanDutyDept.getId()));
                    first.ifPresent(pcPlanDutyDept -> pcPlanDutyDeptVO.setPushId(pcPlanDutyDept.getPushId()));
                    return pcPlanDutyDeptVO;
                }).collect(Collectors.toList());
            }
        }
        return pcPlanDutyDeptVOList;
    }
}
